package com.molin.extensionTest.getFieldModel;

import java.lang.reflect.Field;

public class Demo1 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
//        method1();
//        method2();
//        method3();
//        method04();

    }

    public static void method04() throws ClassNotFoundException, NoSuchFieldException {
        //获取单个成员变量对象，包括私有成员变量对象
        Class clazz = Class.forName("com.molin.extensionTest.getFieldModel.Student");
        Field age = clazz.getDeclaredField("age");
        System.out.println(age);
        Field money = clazz.getDeclaredField("money");
        System.out.println(money);
    }

    public static void method3() throws ClassNotFoundException, NoSuchFieldException {
        //获取单个公共成员变量对象
        Class clazz = Class.forName("com.molin.extensionTest.getFieldModel.Student");
        Field name = clazz.getField("name");
        System.out.println(name);
        Field age = clazz.getField("age");
        System.out.println(age);
    }

    public static void method2() throws ClassNotFoundException {
        //获取所有成员变量，包括私有成员变量对象
        Class clazz = Class.forName("com.molin.extensionTest.getFieldModel.Student");
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField);
        }
    }

    public static void method1() throws ClassNotFoundException {
        //获取全部公共成员变量对象

        //1.获取class对象
        Class clazz = Class.forName("com.molin.extensionTest.getFieldModel.Student");

        //2.获取Filed对象
        Field[] fields = clazz.getFields();
        for (Field field : fields) {
            System.out.println(field);
        }
    }
}
